/*

    thermcap the thermal data collector program.
    thermcap collects CPU heating data and stores it in a csv and svg-graphic file
    Copyright (C) 2011  Giovanni Santostefano

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "queue.h"

/*initialize the b_queue*/
struct b_queue* b_queue_init()
{
	struct b_queue *temp=NULL;
	temp = malloc(sizeof(struct b_queue));
	if(!temp) return;
	
	temp->head=NULL;
	temp->tail=NULL;
	
	return temp; 
}

/*destroy the b_queue*/
void b_queue_free(struct b_queue *bq)
{
	if(!bq)return;
	
	/*remove the items remained*/
	while(bq->head)
	{
		b_queue_delelement(bq);
	}
}

/*add an element at the end of the queue*/
void b_queue_addelement(struct b_queue *bq, QITEM element)
{
	struct b_queue_item *item=NULL;
	
	if(!bq)return;
	
	item=malloc(sizeof(struct b_queue_item));
	if(!item) return;
	
	item->cput=element;
	item->next=NULL;
	
	if(!bq->tail)
	{
		item->prev=NULL;
		bq->tail=item;
		bq->head=item;
	}
	else
	{
		item->prev=bq->tail;
		bq->tail->next=item;
		bq->tail=item;
	}
}

/*remove an element at the head of the queue*/
void b_queue_delelement(struct b_queue *bq)
{
	struct b_queue_item *temp=NULL;
	
	if(!bq)return;
	
	temp=bq->head;
	if(bq->head) 
	{
		bq->head=bq->head->next;
		free(temp);
	}
}

/*get the head of the queue*/
QITEM b_queue_gethead(struct b_queue *bq)
{
	if(!bq)return 0;
	
	return bq->head->cput;
	
}
